package controller;

import com.google.gson.Gson;
import dao.INewDao;
import dao.NewDaoImpl;
import vo.News;
import vo.Student;

import javax.jws.WebService;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name="StudentServlet",value = "/studentServlet")
public class StudentServlet extends HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
         //解决中文乱码问题
        request.setCharacterEncoding("gbk");
        response.setCharacterEncoding("text/html;charset=gbk");
        response.setCharacterEncoding("gbk");
        PrintWriter writer=response.getWriter();
        INewDao dao=new NewDaoImpl();
        List<News> list= null;
        try {
            list = dao.getAllNew();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


//        list.add(new Student(1,"张三","男",21));
//        list.add(new Student(2,"李四","男",21));
//        list.add(new Student(3,"王五","男",21));
        Gson gson=new Gson();
        String s = gson.toJson(list);
        writer.println(s);

    }
}
